<?php
header("Content-type:text/html;charset=utf8");

echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>姓</th><th>名</th><th>邮箱</th><th>备注</th></tr>";


class TableRows extends RecursiveIteratorIterator 
{
    public function __construct($it)
    {
        parent::__construct($it, self::LEAVES_ONLY); 
    }
    public function current()
    {
        return "<td style='width: 150px; border: 1px solid black;'>" . parent::current() . "</td>";
    }
    public function beginChildren()
    {
        echo "<tr>";
    }
    public function endChildren()
    {
        echo "</tr>" . "";
    }
}

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "bbv";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM bbv");
    $stmt->execute();

    // 设置关联数组
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    // 循环这个数组
    foreach (new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k => $v) {
        echo $v;
    }
    $dsn = null;
} catch (PDOException $e) {
    echo "Error:" . $e->getMessage();

}
$conn = null;
echo "</table>";
